﻿using Engine;
using Game;
using GameEntitySystem;
using TemplatesDatabase;

public class ComponentSummoneeMeleeBehavior : ComponentSummoneeBehavior, IUpdateable
{
    public ComponentBody m_target;

    public SubsystemBodies m_subsystemBodies;

    public ComponentSummonee m_componentSummonee;

    public SubsystemGameInfo m_subsystemGameInfo;

    public double m_lastHitTime;

    private float m_importanceLevel;

    public Game.Random m_random = new Game.Random();

    public UpdateOrder UpdateOrder => (UpdateOrder)2;

    public override bool IsActive => true;

    public override float ImportanceLevel => m_importanceLevel;

    public float FindTarget()
    {
        if (m_componentSummonee.m_lord.m_target != null)
        {
            ComponentHealth health = m_componentSummonee.m_lord.m_target.Entity.FindComponent<ComponentHealth>();
            if (health == null || health.Health <= 0f)
            {
                m_componentSummonee.m_lord.m_target = null;
                return 0f;
            }
            m_importanceLevel = 200f;
            m_target = m_componentSummonee.m_lord.m_target;
            return 200f;
        }
        ComponentBody health2 = null;
        float num = 20f;
        foreach (ComponentBody body in m_subsystemBodies.Bodies)
        {
            float num2 = ScoreTarget(body);
            if (num2 < num)
            {
                num = num2;
                health2 = body;
            }
        }
        if (health2 != null)
        {
            m_target = health2;
            return m_importanceLevel = 200f - 10f * num;
        }
        return 0f;
    }

    public float ScoreTarget(ComponentBody body)
    {
       
        ComponentChaseBehavior health = body.Entity.FindComponent<ComponentChaseBehavior>();
        if (health == null)
        {
            return 100f;
        }
        ComponentHealth health2 = body.Entity.FindComponent<ComponentHealth>();
        if (health2 == null || health2.Health <= 0f)
        {
            return 100f;
        }
        if (health.Target != m_componentSummonee.m_lord.Entity.FindComponent<ComponentCreature>())
        {
            return 100f;
        }
        BoundingBox boundingBox = body.BoundingBox;
        Vector3 health3 = 0.5f * (boundingBox.Min + boundingBox.Max);
        Vector3 health4 = m_componentSummonee.VectorFromLord;
        float num = health4.Length();
        health3 = m_componentSummonee.Position - health3;
        float num2 = health4.Length();
        return num + num2 + m_random.Float(0f, 1f);
    }

    public bool IsBodyInAttackRange(ComponentBody target)
    {
       
        if (target == null)
        {
            return false;
        }
        BoundingBox boundingBox = target.BoundingBox;
        Vector3 position = m_componentSummonee.Position;
        Vector3 health = 0.5f * (boundingBox.Min + boundingBox.Max) - position;
        float num = health.Length();
        Vector3 health2 = health / num;
        float num2 = 0.5f * (boundingBox.Max.X - boundingBox.Min.X);
        float num3 = 0.5f * (boundingBox.Max.Y - boundingBox.Min.Y);
        if (MathUtils.Abs(health.Y) < num3 * 0.99f)
        {
            if (num < num2)
            {
                return true;
            }
        }
        else if (num < num3 && MathUtils.Abs(Vector3.Dot(health2, Vector3.UnitY)) > 0.8f)
        {
            return true;
        }
        return false;
    }

    public override void Load(ValuesDictionary valuesDictionary, IdToEntityMap idToEntityMap)
    {
        Load(valuesDictionary, idToEntityMap);
        m_subsystemBodies = Project.FindSubsystem<SubsystemBodies>(true);
        m_componentSummonee = Entity.FindComponent<ComponentSummonee>(true);
        m_subsystemGameInfo = Project.FindSubsystem<SubsystemGameInfo>(true);
        m_componentSummonee.m_behaviors.Add(this);
    }

    public void Update(float dt)
    {
        m_importanceLevel = FindTarget();
    }

    public override void UpdateWhenActive(float dt)
    {
        
        if (!(m_subsystemGameInfo.TotalElapsedGameTime - m_lastHitTime < 0.66))
        {
            BoundingBox boundingBox = m_target.BoundingBox;
            Vector3 val = 0.5f * (boundingBox.Min + boundingBox.Max);
            if (IsBodyInAttackRange(m_target))
            {
                ComponentMiner.AttackBody(m_target, m_componentSummonee.m_lord.Entity.FindComponent<ComponentCreature>(), m_componentSummonee.Position, m_componentSummonee.m_velocity, 10f, true);
                m_lastHitTime = m_subsystemGameInfo.TotalElapsedGameTime;
            }
            else
            {
                m_componentSummonee.m_velocity = m_componentSummonee.m_flySpeed * Vector3.Normalize(val - (m_componentSummonee).Position);
            }
        }
    }
}
